package com.rentapark.app;


import android.app.ListActivity;
import java.util.ArrayList;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;
import java.util.List;

import data.DB;
import data.ListAdapter;
import data.ParkingSpot;

/*this Activity extends a listActivity for the user to see the list of requests he made*/
public class ListOfRequests extends ListActivity{
	DB parking_db;
	
	public void onCreate(Bundle savedInstanceState){   
		super.onCreate(savedInstanceState);
		ListView list = getListView();
        list.setClickable(true);
        //get DB instance
        parking_db=DB.getInstance();
        final List<ParkingSpot> requestList = new ArrayList<ParkingSpot>();
        int ctr=0;
        //add to the "requestList" all the parking spots which's status is 1==requested
        for(int i=0;i<parking_db.dbList.list.size(); i++) {
        	if(parking_db.dbList.list.get(i).status==1)
        	{
        		requestList.add(new ParkingSpot(parking_db.dbList.list.get(i).address,parking_db.dbList.list.get(i).timeToAvailable,parking_db.dbList.list.get(i).prize,parking_db.dbList.list.get(i).ownerName,parking_db.dbList.list.get(i).ownerGrade));
        		ctr++;
        	}
        		
        }
        if(ctr==0)//if the user hasn't performed any requests
        {
        	Toast toast = Toast.makeText(ListOfRequests.this, "You haven't requested any parking spot yet", Toast.LENGTH_SHORT);
			toast.show();
			finish();
        }
        ListAdapter adapter = new ListAdapter(this, requestList);

        list.setOnItemClickListener(new OnItemClickListener() {

			//onItemClick: when the user clicks on an item from the list, show the parking spot's address in a toast msg
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long index) {
                System.out.println("sadsfsf");
                Toast toast = Toast.makeText(ListOfRequests.this, requestList.get(position).address, Toast.LENGTH_LONG);
                toast.show();
				
			}
        });

        list.setAdapter(adapter);



	}
	public void onStart(){
		super.onStart();
		parking_db=DB.getInstance();
		Log.d("onStart", "ListSubs");

	}
}